package org.mifos.boss.ui.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.mifos.boss.application.model.UserCredentials;
import org.mifos.boss.domainmodel.User;
import org.mifos.boss.service.UserServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController extends AbstractController {

	static Logger logger = Logger.getLogger(LoginController.class);

	@Autowired
	@Qualifier("userService")
	private UserServiceInterface userService;

	@RequestMapping(value = "/bossshowuser", method = RequestMethod.GET)
	public String listUsers(Map<String, Object> map) {
		map.put("userList", userService.listUser());

		return "bossshowuser";
	}

	/*
	 * @RequestMapping(value = "/index") public ModelAndView
	 * listUser(@ModelAttribute("user") User user) { ModelAndView mav = new
	 * ModelAndView(); Map<String, Object> userList = new HashMap<String,
	 * Object>(); userList.put("userList", userService.listUser());
	 * mav.addObject("userList", userList);
	 * mav.setViewName("bossusermanagement");
	 * 
	 * return mav; }
	 */
	@RequestMapping(value = "/validateUser", method = RequestMethod.POST)
	public String validateUser(@ModelAttribute("user") User user,
			BindingResult result, HttpSession session) {
		logger.debug("Received request to login.");
		boolean validuser = userService.validateUser(user);
		if (validuser == true) {
			logger.debug("Successfully logged.");
			session.setAttribute("currentUser", user);
			return "redirect:/bossshowuser.htm";
		} else {
			logger.debug("Failed to login.");
			return "redirect:/login.htm";
		}		  		
	}

	@RequestMapping(value = "/bossadduser", method = RequestMethod.GET)
	public String showUsers(Map<String, Object> map) {
		map.put("user", new User());

		return "bossadduser";
	}

	@RequestMapping(value = "/adduser", method = RequestMethod.POST)
	public String addContact(@ModelAttribute("user") User user,
			BindingResult result) {
		
		boolean retSuccess = userService.addUser(user);
		if (retSuccess == true) {
			logger.debug("Successfully Create User.");
			return "redirect:/bossshowuser.htm";
		} else {
			logger.debug(" Failed to Create User.");
			return "redirect:/bossadduser.htm";
		}

	}

	@RequestMapping("/delete/{userid}")
	public String deleteUser(@PathVariable("userid") String userid) {
		logger.info("Delete User with userid = " + userid);	
		boolean retSuccess = userService.removeUser(userid);	    

		if (retSuccess == true) {
			logger.debug("Successfully Delete User.");
		} else {
			logger.debug(" Failed to Delete User.");
		}
		return "redirect:/bossshowuser.htm";
	}

	// >>>>>
	/*
	 * @RequestMapping(value = "/show/{userid}", method = RequestMethod.GET)
	 * public ModelAndView showContact(@PathVariable("userid") String userid) {
	 * User user = userService.getUser(userid);
	 * 
	 * ModelAndView mav = new ModelAndView(); // Use the view named "show" to
	 * display the data mav.setViewName("bossadduser"); // Add a model object to
	 * be displayed by the view mav.addObject("user", user); return mav; }
	 * 
	 * @RequestMapping("/edit/{userid}") public ModelAndView
	 * editContact(@PathVariable("userid") String userid, User user) { user =
	 * userService.getUser(userid);
	 * 
	 * ModelAndView mav = new ModelAndView(); // Use the view named "edit" to
	 * display the data mav.setViewName("bossadduser"); // Add a model object to
	 * be displayed by the view mav.addObject("user", user); return mav; }
	 */
	// >>>>>
	/**
	 * Adds a new customer by delegating the processing to CustomerServiceImpl.
	 * Displays a confirmation JSP page
	 * 
	 * @return the name of the JSP page
	 */
	@RequestMapping(value = "/login")
	public String login(Model model) {

		logger.debug("Received request to login");
		model.addAttribute("user", new User());
		return "login";
	}

	@RequestMapping(value = "/validateUserold")
	public String validateUser(
			@ModelAttribute("UserCredentials") UserCredentials user,
			HttpSession session) {
		user.getJ_username();// check if the values are coming... i checked and
								// the values are coming to this point.
		user.getJ_username();
		ModelAndView model = new ModelAndView();
		// code where we check the DB for user credentials
		logger.debug("Received request to login");
		model.addObject("user", new User());

		session.setAttribute("currentUser", user);

		// go to DB and validate user
		// customerBusiness.validateUser(user);in business validate this

		// if its a valid user

		return "bossDashboard";
	}

}
